﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Zhaoxi.TakeaWay.App.Utility;
using Zhaoxi.TakeaWay.App.Utility.Filters;
using Zhaoxi.TakeaWay.App.Utility.SwaggerExt;
using Zhaoxi.TakeaWay.DataAccess;

using Microsoft.EntityFrameworkCore;
using Zhaoxi.TakeaWay.BusinessInterface;
using Zhaoxi.TakeaWay.BusinessService;
using Zhaoxi.TakeaWay.Model;
using AutoMapper;
using Zhaoxi.TakeaWay.ModelsDto;

namespace Zhaoxi.TakeaWay.App.Controllers
{
    /// <summary>
    /// 外卖平台首页api
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
    public class PlatformIndexController : ControllerBase
    {

        //private DbContext _DbContext;

        ///// <summary>
        ///// 构造函数
        ///// </summary>
        ///// <param name="dbContext"></param>
        //public PlatformIndexController(DbContext dbContext)
        //{
        //    _DbContext = dbContext;
        //}


        private ICategoryService _ICategoryService;
        private IMapper _IMapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="iCategoryService"></param>
        public PlatformIndexController(ICategoryService iCategoryService, IMapper iMapper)
        {
            _ICategoryService = iCategoryService;
            _IMapper = iMapper;
        }


        /// <summary>
        /// 获取类目的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //[CorsActionFilterAttribute]
        [Route("GetCategoryList/{isAll}")]
        public IActionResult GetCategoryList([FromServices] ICategoryService categoryService, bool isAll)
        {
            //Web项目直接去调用EFCore 访问数据库;
            //后续的业务逻辑都需要写到控制器里面
            //1. 参数的验证
            //2. 日志的记录
            //3. 查询一条数据
            //4. 删除一条数据

            //List<Category> datalist;
            //using (TakeaWayDbContext context = new TakeaWayDbContext())
            //{
            //    datalist= context.Categories.ToList();
            //} 

            {
                //ICategoryServcie category = new CategoryServcie();
                //category.Add();
                //category.Update();
                //category.Query();
                //category.Delete();
            }


            //List<Category> datalist = _DbContext.Set<Category>().ToList();
            ////HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); 
             
            #region 伪代码 
            //List<Category> datalist = new List<Category>()
            //{
            //    new Category() { Id = 1,Title="美食外卖",Icon="#icon-meishi"},
            //    new Category() { Id = 2,Title="超市便利",Icon="#icon-chaoshi"},
            //    new Category() { Id = 3,Title="水果",Icon="#icon-shuiguo"},
            //    new Category() { Id = 4,Title="买菜",Icon="#icon-shucaiqu"},
            //    new Category() { Id = 5,Title="买药",Icon="#icon-yaoxiang"},
            //    new Category() { Id = 6,Title="电影/演出",Icon="#icon-dianying"},
            //    new Category() { Id = 7,Title="景点/门票",Icon="#icon-a-iconjingdian"},
            //    new Category() { Id = 8,Title="周边游/旅游",Icon="#icon-lvyou"},
            //    new Category() { Id = 9,Title="健身/游泳",Icon="#icon-jianshen"},
            //    new Category() { Id =10,Title="跑腿",Icon="#icon-_waimai"},
            //    new Category() { Id =11,Title="丽人/美发",Icon="#icon-meifa"},
            //    new Category() { Id =12,Title="医学美容",Icon="#icon-nvren"},
            //    new Category() { Id =13,Title="亲子/乐园",Icon="#icon-qinzi"},
            //    new Category() { Id =14,Title="签到领现金",Icon="#icon-huiyuan1"},
            //    new Category() { Id =15,Title="周边游/旅游",Icon="#icon-lvyou"},
            //    new Category() { Id =16,Title="健身/游泳",Icon="#icon-jianshen"},
            //    new Category() { Id =17,Title="跑腿",Icon="#icon-_waimai"},
            //    new Category() { Id =18,Title="丽人/美发",Icon="#icon-meifa"},
            //    new Category() { Id =19,Title="医学美容",Icon="#icon-nvren"},
            //    new Category() { Id =20,Title="亲子/乐园",Icon="#icon-qinzi"},
            //    new Category() { Id =21,Title="签到领现金",Icon="#icon-huiyuan1"}
            //}; 
            #endregion

            //ICategoryService categoryService = new CategoryService(new TakeaWayDbContext()); 

            //IOC容器的使用

            List<Category> datalist = _ICategoryService.Query<Category>(c => true).ToList();
            var list = categoryService.Query<Category>(c => true).ToList();
            if (isAll == false)
            {
                datalist = datalist.Take(14).ToList();
            }

            //不使用作为和数据库映射的实体来作为返回的实体结构;
            //automapper

            //Category --- CategoryDTO
            //1.nuget引入Automapper  AutoMapper.Extensions.Microsoft.DependencyInjection
            //2.配置转换规则
            //3.规则生效
            //4.注入Mapper, Map实体映射

            List<CategoryDTO> categoryDTOList = _IMapper.Map<List<Category>, List<CategoryDTO>>(datalist);

            ApiDataResult<List<CategoryDTO>> result = new ApiDataResult<List<CategoryDTO>>()
            {
                Success = true,
                Message = "获取类目数据",
                Data = categoryDTOList
            };
            return new JsonResult(result);
        }


        /// <summary>
        /// 获取轮播图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRotationChart")]
        public IActionResult GetRotationChart()
        {
            //var datalist = new List<RotationChart>()
            //    {
            //        new RotationChart()
            //        {
            //           CreateTime= DateTime.Now,
            //           ImageUrl="images/Banner/banner-1.jpg",
            //           Id=1,
            //        },
            //        new RotationChart()
            //        {
            //           CreateTime= DateTime.Now,
            //           ImageUrl="images/Banner/banner-2.jpg",
            //           Id=2,
            //        },
            //        new RotationChart()
            //        {
            //           CreateTime= DateTime.Now,
            //           ImageUrl="images/Banner/banner-3.jpg",
            //           Id=3,
            //        },
            //        new RotationChart()
            //        {
            //           CreateTime= DateTime.Now,
            //           ImageUrl="images/Banner/banner-4.jpg",
            //           Id=4,
            //        },
            //        new RotationChart()
            //        {
            //           CreateTime= DateTime.Now,
            //           ImageUrl="images/Banner/banner-5.jpg",
            //           Id=5,
            //        },

            //    };


            List<RotationChart> datalist = _ICategoryService.Query<RotationChart>(c => true).ToList();
            List<RotationChartDTO> list = _IMapper.Map<List<RotationChart>, List<RotationChartDTO>>(datalist);
            return new JsonResult(new ApiDataResult<List<RotationChartDTO>>()
            {
                Data = list,
                Success = true
            });
        }

        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("ProductList")]
        public IActionResult ProductList()
        {
            //var datalist = new List<Product>()
            //    {
            //      new Product()
            //      {
            //        Id=1,
            //        Image="Images/product/main-1.png",
            //        Title="榴莲+烤鸡+培根+甄选+夏威夷+水果",
            //        Score="4.8分",
            //        MonthSales="月售3565",
            //        Deliveryquota="起送￥30",
            //        Deliveryfee="免费配送",
            //        Fullreduction="满50减9"
            //      },
            //      new Product()
            //      {
            //        Id=2,
            //        Image="Images/product/main-2.png",
            //        Title="榴莲+烤鸡+培根+甄选+夏威夷+水果",
            //        Score="4.8分",
            //        MonthSales="月售3565",
            //        Deliveryquota="起送￥30",
            //        Deliveryfee="免费配送",
            //        Fullreduction="满50减9"
            //      },
            //      new Product()
            //      {
            //        Id=3,
            //        Image="Images/product/main-2.png",
            //        Title="榴莲+烤鸡+培根+甄选+夏威夷+水果",
            //        Score="4.8分",
            //        MonthSales="月售3565",
            //        Deliveryquota="起送￥30",
            //        Deliveryfee="免费配送",
            //        Fullreduction="满50减9"
            //      },
            //      new Product()
            //      {
            //        Id=4,
            //        Image="Images/product/main-3.png",
            //        Title="榴莲+烤鸡+培根+甄选+夏威夷+水果",
            //        Score="4.8分",
            //        MonthSales="月售3565",
            //        Deliveryquota="起送￥30",
            //        Deliveryfee="免费配送",
            //        Fullreduction="满50减9"
            //      },
            //      new Product()
            //      {
            //        Id=5,
            //        Image="Images/product/main-4.png",
            //        Title="榴莲+烤鸡+培根+甄选+夏威夷+水果",
            //        Score="4.8分",
            //        MonthSales="月售3565",
            //        Deliveryquota="起送￥30",
            //        Deliveryfee="免费配送",
            //        Fullreduction="满50减9"
            //      }
            //    };

            List<Product> datalist = _ICategoryService.Query<Product>(c => true).ToList();
            List<ProductDTO> list = _IMapper.Map<List<Product>, List<ProductDTO>>(datalist); 
            return new JsonResult(new ApiDataResult<List<ProductDTO>>()
            {
                Success = true,
                Message = "获取推广的商品数据",
                Data = list
            });
        }

    }
}
